<!--添加简答题-->
<template>
  <el-dialog
    title="添加简答题"
    :visible.sync="addVisible"
    width="60%"
    :show-close="false"
    :close-on-click-modal="false">

    <el-form :model="DataForm" status-icon ref="DataForm" :rules="DataForm_rules" label-width="60px" class="demo-ruleForm">
      <el-form-item label="科目：" prop="f_sb_id">
        <div class="block">
          <el-cascader
            placeholder="请选择"
            :options="options"
            filterable
            v-model="DataForm.f_sb_id"
          ></el-cascader>
        </div>
      </el-form-item>
      <div v-for="(obj,index) in DataForm.formDataList" :key="index">
        <el-row>
          <el-col :span="20">
            <el-form-item label="题目:" prop="sa_title">
              <el-input type="textarea" v-model="DataForm.formDataList[index].sa_title" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="3">
            <el-button style="float: right; padding: 3px 0;margin-left: 15px;" type="danger" size="mini" @click="deleteDataForm(index)">删除</el-button>
            <el-button style="float: right; padding: 3px 0" type="primary" size="mini" @click="addDataForm(index)">增加</el-button>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="答案:" prop="saa_answer">
              <el-input type="textarea" v-model="DataForm.formDataList[index].shortAnswerAns.saa_answer" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider></el-divider>
      </div>
    </el-form>

    <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog" size="mini">取 消</el-button>
        <el-button type="primary" @click="submitData" size="mini">确 定</el-button>
      </span>
  </el-dialog>
</template>
<script>
  import {getAllSubject,addShortAnswer} from '@/api/questions'
  import ElRow from "element-ui/packages/row/src/row";
  import ElDivider from "../../../../../node_modules/element-ui/packages/divider/src/main";
  export default{
    components: {
      ElDivider,
      ElRow},
    props:["showAddVisible"],
      mounted(){
        this.c_getAllSubject();
      },
      data(){
          return{
            addVisible:this.showAddVisible,
            DataForm:{
              formDataList:[
                {sa_title:"",sb_id:0,shortAnswerAns:{saa_answer:""}}
              ],
              f_sb_id:0
            },
            options:[], //选择所属科目

            DataForm_rules:{

            }
          }
      },

      methods:{
        submitData(){
          this.$refs["DataForm"].validate((valid) =>{
            if(valid){
              for(let i = 0;i < this.DataForm.formDataList.length;i ++){
                this.DataForm.formDataList[i].sb_id = parseInt(this.DataForm.f_sb_id[2])
              }
              addShortAnswer(this.DataForm.formDataList).then(res=>{
                if(res.code == 200){
                  this.$message({
                    type: 'success',
                    message: '添加成功'
                  });
                  this.closeDialog();
                }else{
                  this.$message({
                    type: 'error',
                    message: '添加失败'
                  });
                }
              });
            }else{

            }
          });
        },

        closeDialog(){
            this.addVisible = false;
            this.$emit("closeAddDialog","add");
        },

        addDataForm(index){
          this.DataForm.formDataList.push({sa_title:"",sb_id:0,shortAnswerAns:{saa_answer:""}})
        },

        deleteDataForm(index){
          if(this.DataForm.formDataList.length > 1){
            this.DataForm.formDataList.splice(index,1)
          }
        },

        c_getAllSubject(){
          getAllSubject().then(res=>{
            if(res.code == 200){
              for(let i = 0;i < res.data.length;i ++){
                let tLevel = {value:res.data[i].l_id,label:res.data[i].l_name};
                this.options.push(tLevel);
                if(res.data[i].list != null && res.data[i].list.length > 0){
                  let gList = res.data[i].list;
                  let children = [];
                  for(let j = 0;j < gList.length;j ++){
                    let gGroup = {value:gList[j].g_id,label:gList[j].g_name};
                    children.push(gGroup);

                    if(gList[j].list != null && gList[j].list.length > 0){
                      let sList = gList[j].list;
                      let children1 = [];

                      for(let n = 0;n < sList.length;n ++){
                        let subject = {value:sList[n].sb_id,label:sList[n].sb_name};
                        children1.push(subject);
                      }
                      gGroup.children = children1;
                    }
                  }
                  tLevel.children = children;
                }
              }
            }
          });
        },
      }
  }
</script>
